﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models.BLL;
using tracker.Models;
using System.Data;
using tracker.Models.DTO;

namespace tracker.Controllers
{
    public class HandlerGetSpeedInfoController : Controller
    {
       /// <summary>
       /// Get All Alarm Define
       /// </summary>
       /// <returns></returns>
        [CallAjaxFilter]
        public JsonStringResult Index(string fromtime, string totime, string snid)
        {

            string result = "";
            string _username = HttpContext.Session[Common.USER].ToString();
            string _language = HttpContext.Session[Common.LANGUAGE].ToString();
            string timezone = HttpContext.Session[Common.TIMEZONE].ToString();
            _language = _language == string.Empty ? "vi" : _language;

            if (fromtime == null || totime == null || snid == null)
            {
                result = "{totalCount:0,data:[{}]}";
            }
            else
            {
                if (ClassCommon.classValidate.isDateTime(fromtime) && ClassCommon.classValidate.isDateTime(totime))
                {
                    List<EGetSpeedInfo> list = new List<EGetSpeedInfo>();
                    EGetSpeedInfo ent = new EGetSpeedInfo();
                    ent.SpeedSegment = "0-20 km/h";
                    ent.Total = TrackLogBLL.GetTotalSpeedInfoCricleChart(new string[]{"DeviceKeySecurity",
                                                                                      "speedFrom",
                                                                                      "speedTo",
                                                                                      "FromTime",
                                                                                      "ToTime"},
                                                                         new object[]{snid,
                                                                                      0,
                                                                                      20,
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(fromtime),timezone),
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(totime),timezone)
                                                                         });
                    if (ent.Total > 0)
                        list.Add(ent);

                    ent = new EGetSpeedInfo();
                    ent.SpeedSegment = "20-40 km/h";
                    ent.Total = TrackLogBLL.GetTotalSpeedInfoCricleChart(new string[]{"DeviceKeySecurity",
                                                                                      "speedFrom",
                                                                                      "speedTo",
                                                                                      "FromTime",
                                                                                      "ToTime"},
                                                                         new object[]{snid,
                                                                                      20,
                                                                                      40,
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(fromtime),timezone),
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(totime),timezone)
                                                                         });
                    if (ent.Total > 0)
                        list.Add(ent);

                    ent = new EGetSpeedInfo();
                    ent.SpeedSegment = "40-60 km/h";
                    ent.Total = TrackLogBLL.GetTotalSpeedInfoCricleChart(new string[]{"DeviceKeySecurity",
                                                                                      "speedFrom",
                                                                                      "speedTo",
                                                                                      "FromTime",
                                                                                      "ToTime"},
                                                                         new object[]{snid,
                                                                                      40,
                                                                                      60,
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(fromtime),timezone),
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(totime),timezone)
                                                                         });
                    if (ent.Total > 0)
                        list.Add(ent);

                    ent = new EGetSpeedInfo();
                    ent.SpeedSegment = "60-80 km/h";
                    ent.Total = TrackLogBLL.GetTotalSpeedInfoCricleChart(new string[]{"DeviceKeySecurity",
                                                                                      "speedFrom",
                                                                                      "speedTo",
                                                                                      "FromTime",
                                                                                      "ToTime"},
                                                                         new object[]{snid,
                                                                                      60,
                                                                                      80,
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(fromtime),timezone),
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(totime),timezone)
                                                                         });
                    if (ent.Total > 0)
                        list.Add(ent);

                    ent = new EGetSpeedInfo();
                    ent.SpeedSegment = "80-100 km/h";
                    ent.Total = TrackLogBLL.GetTotalSpeedInfoCricleChart(new string[]{"DeviceKeySecurity",
                                                                                      "speedFrom",
                                                                                      "speedTo",
                                                                                      "FromTime",
                                                                                      "ToTime"},
                                                                         new object[]{snid,
                                                                                      80,
                                                                                      100,
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(fromtime),timezone),
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(totime),timezone)
                                                                         });
                    if (ent.Total > 0)
                        list.Add(ent);

                    ent = new EGetSpeedInfo();
                    ent.SpeedSegment = ">100 km/h";
                    ent.Total = TrackLogBLL.GetTotalSpeedInfoCricleChart(new string[]{"DeviceKeySecurity",
                                                                                      "speedFrom",
                                                                                      "speedTo",
                                                                                      "FromTime",
                                                                                      "ToTime"},
                                                                         new object[]{snid,
                                                                                      100,
                                                                                      500,
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(fromtime),timezone),
                                                                                      ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(totime),timezone)
                                                                         });
                    if (ent.Total > 0)
                        list.Add(ent);


                    if (list.Count > 0)
                    {
                        result = Utilities.getJsonString(list, "data:", 1);
                    }
                    else
                    {
                        result = "{totalCount:0,data:[{}]}";
                    }
                }
                else
                {
                    result = "{totalCount:0,data:[{}]}";
                }
            }
            return new JsonStringResult(result);
        }
    }
}
